package com.dyz.leetcode;

import java.util.HashMap;
import java.util.Map;

public class SingleNumber136 {
    public int singleNumber(int[] nums) {
        //异或操作
        //根据定义我们很容易获得异或两个特性：
        //恒等律：X ⊕ 0 = X 归零律：X ⊕ X = 0
        int res = 0;
        for(int num:nums){
            res = res^num;
        }
        return res;
    }

    public int singleNumber1(int[] nums) {
        Map<Integer, Integer> map = new HashMap<>();
        for(int i=0; i<nums.length; i++){
            map.put(nums[i], map.getOrDefault(nums[i], 0)+1);
        }
        int res=0;
        for(int k:map.keySet()){
            if(map.get(k)==1){
                res=k;
                break;
            }
        }
        return res;
    }
}
